package com.i2863.tools.weixinmp.filter;



import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by xuweiwei on 2017/8/3.
 */
public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        String uri = req.getRequestURI();
        //静态资源通过
        if(uri.endsWith("css") || uri.endsWith("js")  || uri.endsWith("png") || uri.endsWith("jpg")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }

        //涉及登录和注销的页面通过
        if(uri.contains("login") || uri.contains("logout")){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        String userName =  String.valueOf(req.getSession().getAttribute("username"));
        if(userName!=null && userName.length()>0 && !"null".equals(userName)){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        resp.sendRedirect("/login.html");


    }

    @Override
    public void destroy() {

    }
}
